package com.wc.alorithm_luogu.P1518;

import java.util.Scanner;

/**
 * @author congege
 * @create 2023:04:26 5:35
 * @describe https://www.luogu.com.cn/problem/P1518
 * 两只塔姆沃斯牛 The Tamworth Two
 * 做的不好
 */
public class Main {
    public static int[][] orientation = new int[4][2];

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = 10;
        int m = 10;
        char[][] map = new char[m][n];


        // 为了顺时针的上 右 下 左
        // 向上
        orientation[0] = new int[]{-1, 0};
        // 向右
        orientation[1] = new int[]{0, 1};
        // 向下
        orientation[2] = new int[]{1, 0};
        // 向左
        orientation[3] = new int[]{0, -1};

        // 画出牧场
        String row;
        for (int i = 0; i < m; i++) {
            row = sc.nextLine();
            map[i] = row.toCharArray();
        }

        // 数组设置成三个，第三个表示方向，方便修改
        int[] farmerXY = new int[3];
        int[] cowXY = new int[3];


        // 找到F C 位置
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (map[i][j] == 'F') {
                    farmerXY[0] = i;
                    farmerXY[1] = j;
                }
                if (map[i][j] == 'C') {
                    cowXY[0] = i;
                    cowXY[1] = j;
                }
            }
        }


        int step = 0;
        while (cowXY[0] != farmerXY[0] || cowXY[1] != farmerXY[1]) {
            step++;
//            map[cowXY[0]][cowXY[1]]='.';
            move(map, cowXY);
//            map[cowXY[0]][cowXY[1]] = 'C';
//            map[farmerXY[0]][farmerXY[1]] = '.';
            move(map, farmerXY);
//            map[farmerXY[0]][farmerXY[1]] = 'F';
//            show(map);
            // 为什么当步骤大于160005时就表示找不到了
            // 每一个物体方向时4个，100个格子，就是有400种可能
            // 两个一共400 * 400 = 160000
            // 但是我们需要超过他一点，确保
            if (step >= 160005) {
                System.out.println(0);
                return;
            }
        }
        System.out.println(step);
    }

    /**
     * 移动一次或者转向
     *
     * @param map    牧场
     * @param object 需要移动的物体
     */
    public static void move(char[][] map, int[] object) {
        int stepX = object[0] + orientation[object[2]][0];
        int stepY = object[1] + orientation[object[2]][1];
        // 遇到上边界或者是左边界
        if (stepX == -1 || stepY == -1) {
            object[2] = (object[2] + 1) % orientation.length;
            return;
        }

        // 遇到下边界 或者是 右边界
        if (stepX == map.length || stepY == map.length) {
            object[2] = (object[2] + 1) % orientation.length;
            return;
        }
        // 遇到障碍物
        if (map[stepX][stepY] == '*') {
            object[2] = (object[2] + 1) % orientation.length;
            return;
        }
        object[0] = stepX;
        object[1] = stepY;
    }

    public static void paint(char[][] map, int[] object, boolean[][] mapV) {
        move(map, object);
        // 记录走过的位置
        mapV[object[0]][object[1]] = true;
    }

    /**
     * 撞到障碍物后停止
     *
     * @param map    牧场
     * @param object 移动的物体
     */
    public static void obstacleStop(char[][] map, int[] object) {
        int preX = -1;
        int preY = -1;
        // 当移动后位置没有变的时候就撞到了障碍物，才能开始循环
        while (preX != object[0] || preY != object[1]) {
            move(map, object);
            preX = object[0];
            preY = object[1];
        }
    }


    public static void show(char[][] map) {
        int n = map.length;
        int m = map[0].length;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                System.out.print(map[i][j] + " ");
            }
            System.out.println();
        }
    }
}
